package com.yang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.mapper.UserMapper;
import com.yang.model.Pet;
import com.yang.model.User;
import com.yang.service.PetService;
import com.yang.mapper.PetMapper;
import com.yang.utlis.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 杨凌霄的大眼睛
* @description 针对表【pet】的数据库操作Service实现
* @createDate 2023-05-30 19:15:40
*/
@Service("petService")
public class PetServiceImpl extends ServiceImpl<PetMapper, Pet>
    implements PetService{

    @Resource
    private PetMapper petMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Pet queryPetByUserId(String ownerId) {
        if (StringUtils.isAnyBlank(ownerId)) {
            return null;
        }
        Pet pet = petMapper.selectOne(new LambdaQueryWrapper<Pet>().eq(Pet::getOwnerId, ownerId));
        User user = userMapper.selectById(ownerId); // 获取用户信息
        if (pet == null) { // 如果宠物不存在，新增宠物
            pet = new Pet(UUIDUtil.getUUID(), ownerId, user.getAccount(), 0, 0, 0);
            petMapper.insert(pet);
        }
        return pet;
    }

    @Override
    public Pet updatePetByPetId(String id, String speed, String attack, String defence) {
        if (StringUtils.isAnyBlank(id, speed, attack, defence)) {
            return null;
        }
        // 查询更新前的信息
        Pet pet = petMapper.selectById(id);
        pet.setAttack(Integer.parseInt(attack) + pet.getAttack());
        pet.setSpeed(Integer.parseInt(speed) + pet.getSpeed());
        pet.setDefence(Integer.parseInt(defence) + pet.getDefence());
        petMapper.updateById(pet);
        return pet;
    }
}




